package com.orangelife.mobile.parkingcard.activity;

/**
 * 留言
 */
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;

public class LeaveMessageActivity extends OrangeLifeBaseActivity {
	
	/**标题*/
	private TextView tvTitle;
	/**留言内容*/
	private EditText etMessage;
	/**确认提交*/
	private Button btnSure;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parking_card_leave_message);
		initeView();
		findView();
	}

	/**
	 * 初始化
	 */
	private void initeView() {
		tvTitle = (TextView) findViewById(R.id.tv_title);
		etMessage = (EditText) findViewById(R.id.etMessage);
		btnSure = (Button) findViewById(R.id.btnSure);
	}

	/**
	 * 赋值
	 */
	private void findView() {
		
		tvTitle.setText(R.string.add_building_tip_leave_message);
		btnSure.setOnClickListener(listener);
		etMessage.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				String str = s.toString();
				if(s.length()>0 && s.length() <= 10){
					Pattern p = Pattern.compile("[\u4E00-\u9FA5]+");
					Matcher matcher = p.matcher(str);
					if(!matcher.matches()){
						ToastHelper.getInstance()._toast("最多输入10个汉字");
						str = "";
						etMessage.setText(str);
					}
				}else{
					ToastHelper.getInstance()._toast("最多输入10个汉字");
					if(s.length() > 10){
						String str10 = str.substring(0, 10);
						etMessage.setText(str10);
					}
				}
			}
		});
	}

	/**
	 * 点击监听器
	 */
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			
			switch (view.getId()) {
			case R.id.btnSure:
				
				setMessage();
				
				break;

			default:
				break;
			}
		}
	};

	/**
	 * 提交留言
	 */
	private void setMessage() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("content", etMessage.getText().toString());
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url", Constant.URL_COMMUNITY_MESSAGE);
		rootMap.put("wat", 2);
		new JSONRequest(JSONRequest.POST, rootMap, handler,
				HttpUtil.ACTION_JSON);
	}
	
	/**
	 * 接收处理线程返回的消息
	 */
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.WHAT_ERROR_HTTP:
				ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
				break;
			
			case 2:
			 String	errInfo = JSONHelper.jsonToMap(msg.obj.toString())
						.get("errInfo").toString();
				if (errInfo.equals("OK")) {
					ToastHelper.getInstance()._toast(getResources().getString(R.string.add_community_success));
					finish();
					
				}
				break;
			}
		};
	};
}
